<?php
/* Immanuel192 write this action on 8/12/2009 */
		global $db;
		// Used later, for recalculation and other things.
		if (!defined("SYS_SORTING")) require_once(BASE."subsystems/sorting.php");
		
		$config = $db->selectObject('dailydevotionmodule_config',"location_data='".serialize($loc)."'");
		if ($config == null) {
			$config->items = 5;
			$config->host_location_data='';
		} else {
			
		}
			
		// get module title
		// because if the client switch page, so, that is the action and exponent does not transfer $title and $views
	  $temp=$db->selectObject("container","internal = '" . serialize($loc) . "'");
	  if ($temp)
	  {
	    $title=$temp->title;
	    $view=$temp->view;
    }
    else
    {
      $title="";
      $view="Default";
    }
		// Create the template
		$template = new template("articlemodule",$view,$loc);
		$template->assign("config",$config);
		
		// 8/14/2009: Immanuel192 remove this code to get article module work with page split
    // Get all of the categories for this Article module:
		/*$cats = array();
		$cats = $db->selectObjectsIndexedArray('category', "location_data='".serialize($loc)."'");
		if ($config->enable_categories) {
			if (count($cats) != 0) {
				$template->assign('hasCategories', 1);				
			} else {
				$template->assign('hasCategories', 0);
			}
		} else {
			$template->assign('hasCategories', 0);
		}
		
		$c->name = '';
		$c->id = 0;
		uasort($cats, "exponent_sorting_byRankAscending");
		$cats[0] = $c;
		$template->assign('categories', $cats);
		
		$data = array();
		if ($config->enable_categories == true) {
			foreach ($cats as $id=>$c) {
				//Get all the questions & answers for this article module. ($qnas stands for "Questions 'n Answers")
				$tmp = $db->selectObjects("article","location_data='".serialize($loc)."' AND category_id=".$id);
				$catids = array_keys($cats); // for in_array check only
				for ($i = 0; $i < count($tmp); $i++) {
					if (!in_array($tmp[$i]->category_id,$catids)) {
					//	echo 'Setting cat id to 0<br />';
						$tmp[$i]->category_id = 0;
					}
				}
				usort($tmp, "exponent_sorting_byRankAscending");
				$data[$id] = $tmp;
			}
		} else {
			$tmp = $db->selectObjects("article","location_data='".serialize($loc)."' AND category_id=0");
			usort($tmp, 'exponent_sorting_byRankAscending');
			$data[0] = $tmp;
		}
		*/
		
		// 8/14/2009: Immanuel192 write this code to spilt page
    $temp=$db->selectObjects("article","location_data='".$config->host_location_data ."' ORDER BY rank DESC, id ASC limit 0,{$config->items}");  
		$data[0]=$temp;
		
		// file ID, image id, create_date, ower_id
		for ($i=0; $i<count($data); $i++)
		{
		  for ($j=0; $j<count($data[$i]); $j++)
		  {
		    $value=$data[$i][$j];
		    // ower_id
		    $temp=$db->selectObject('user','id = '. $value->ower_id);
		    $value->username=$temp->firstname . " " . $temp->lastname;
				// for date
				$date_translate = exponent_lang_loadFile('subsystems/forms/controls/yuicalendarcontrol.php');
				$date_time=0;
				$date_time=$value->create_date;
				$datetime = date('l, F d, o g:i a', $date_time);
				// If the language is english, use the default language of yui calendar
				if (USE_LANG != 'eng_US') {
				// Translate days of the week
				// thứ
				$datetime=$date_translate[strtolower(substr(date('l',$date_time),0,2))].", ";
				// ngày
				$datetime.=date('d',$date_time)." ";
				// tháng
				$datetime.=$date_translate[strtolower(date('F',$date_time))]." ";
				// năm
				$datetime.=date('o',$date_time)."; ";
				$datetime.=date('g:i',$date_time)." ";
				// am - pm
				$datetime.=$date_translate[strtolower(date('a',$date_time))];
				}
				$value->create_date=$datetime;
				
				// for image
				//Get the image file if there is one.
          if (isset($value->image_id) && ($value->image_id > 0)) {
              $file = $db->selectObject('file', 'id='.$value->image_id);
              $value->image_path = $file->directory.'/'.$file->filename;
       	  }
       	  else
       	    $value->image_path="";
       	  //Get the background file if there is one.
          if (isset($value->background_id) && ($value->background_id > 0)) {
              $file = $db->selectObject('file', 'id='.$value->background_id);
              $value->background_path = $file->directory.'/'.$file->filename;
       	  }
       	  else
       	    $value->background_path="";
       	
         // update
         $data[$i][$j]=$value;  
      }
    }
		
		$template->assign('submissions', $db->countObjects('article_submission', "location_data='".serialize($loc)."'"));
		$template->assign('data',$data); 
		$template->register_permissions(
			array('administrate','configure','manage'),
			$loc);
		$template->assign('moduletitle', $title);
		//echo "<xmp>";
		//print_r ($data);
		//echo "</xmp>";
		if (!(isset($_REQUEST['flow_has_set']) && ($_REQUEST['flow_has_set']==1)))
  		exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_ACTION);
		$template->output();
?>
